package com.dzm.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dzm.common.exception.Assert;
import com.dzm.common.result.ResponseEnum;
import com.dzm.core.entity.UserBind;
import com.dzm.core.entity.UserInfo;
import com.dzm.core.enums.UserBindEnum;
import com.dzm.core.hfb.FormHelper;
import com.dzm.core.hfb.HfbConst;
import com.dzm.core.hfb.RequestHelper;
import com.dzm.core.mapper.UserBindMapper;
import com.dzm.core.service.IUserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzm.core.service.IUserInfoService;
import com.dzm.core.vo.UserBindVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.FileHandler;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author Dzm
 * @since 2024-09-20
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements IUserBindService {

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public String bind(UserBindVo bindVo, Long userId) {
        String idCard = bindVo.getIdCard();
        //先确认该身份证未与其他用户绑定
        UserBind one = this.getOne(new LambdaQueryWrapper<UserBind>()
                .eq(UserBind::getIdCard, idCard)
                .ne(UserBind::getUserId, userId));
        Assert.isNull(one, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);
        //获取该用户绑定信息
        UserBind userBind = this.getOne(new LambdaQueryWrapper<UserBind>()
                .eq(UserBind::getUserId, userId)
                .eq(UserBind::getIdCard, idCard));

        if(userBind == null) {
            userBind = new UserBind();
            BeanUtils.copyProperties(bindVo, userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            this.save(userBind);

        }else {
            //绑定信息已存在则修改
            BeanUtils.copyProperties(bindVo, userBind);
            this.updateById(userBind);
        }

        //获取返回表单信息
        Map<String, Object> map = new HashMap<>();
        map.put("agentId", HfbConst.AGENT_ID);
        map.put("agentUserId", userId);
        map.put("idCard",bindVo.getIdCard());
        map.put("personalName", bindVo.getName());
        map.put("bankType", bindVo.getBankType());
        map.put("bankNo", bindVo.getBankNo());
        map.put("mobile", bindVo.getMobile());
        map.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        map.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        map.put("timestamp", RequestHelper.getTimestamp());
        map.put("sign", RequestHelper.getSign(map));

        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, map);

        return formStr;
    }

    @Override
    public void bindNotify(Map<String, Object> map) {
        String  userIdStr = (String) map.get("agentUserId");
        if(StrUtil.isNotBlank(userIdStr)) {
            Long userId = Long.parseLong(userIdStr);
            String bindCode = (String) map.get("bindCode");

            //修改用户绑定信息
            UserBind userBind = this.getOne(new LambdaQueryWrapper<UserBind>()
                    .eq(UserBind::getUserId, userId));
            userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
            userBind.setBindCode(bindCode);
            this.updateById(userBind);

            //修改用户信息
            UserInfo userInfo = userInfoService.getOne(new LambdaQueryWrapper<UserInfo>()
                    .eq(UserInfo::getId, userId));
            userInfo.setName(userBind.getName());
            userInfo.setIdCard(userBind.getIdCard());
            userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
            userInfo.setBindCode(bindCode);
            userInfoService.updateById(userInfo);
        }
    }
}
